<?php

class Soul_Request_Application_Http extends Soul_Request_Application_Base {
	
	public function __construct(){
		parent::__construct();
        $GLOBALS[RC] = Soul_Request_Context::getInstance();
        $GLOBALS[TC] = Soul_Template_Context::getInstance();
	}

	protected function beforeInitialize(){
		$this->dispatchEvent('request.beforeInitialize'); 
	}
	
    protected function initialize() {    	
        $this->beforeInitialize();
		$GLOBALS[RC]->setData('request', Soul_Request_Factory::getRequest())
		        	->setData('response', Soul_Request_Factory::getResponse());
        $this->afterInitialize();
    }
    
	protected function afterInitialize(){
		$this->dispatchEvent('request.afterInitialize');
	}    
	
	protected function beforeSessionStart(){
		$this->dispatchEvent('request.beforeSession');
	}	
    
    protected function sessionStart(){       
    	$this->beforeSessionStart();
        Soul_Request_Factory::getSession()->start();
    	$GLOBALS[RC]->setSession(Soul_Request_Factory::getSession())
                    ->setIdentity(Soul_Request_Factory::getIdentity())
                    ->setMessager(Soul_Request_Factory::getMessager());
        $GLOBALS[TC]->setSession(Soul_Request_Factory::getSession())
                    ->setIdentity(Soul_Request_Factory::getIdentity());       
        $this->afterSessionStart();
    }    
    
	protected function afterSessionStart(){
		$this->dispatchEvent('request.afterSession');
	}    
	
	protected function beforeRoute(){
		$this->dispatchEvent('request.beforeRoute');
	}	
    
	protected function route() {
		$this->beforeRoute();
		Soul_Request_Factory::getRouter()->route();
        $GLOBALS[RC]->setData('router', Soul_Request_Factory::getRouter());
        $GLOBALS[TC]->setData('router', Soul_Request_Factory::getRouter());
		$this->afterRoute();
	}

	protected function afterRoute(){
		$this->dispatchEvent('request.afterRoute');
	}
	
	protected function beforeFilter(){
		$this->dispatchEvent('request.beforeFilter');
	}	
	
	protected function filter(){
		$this->beforeFilter();
		Soul_Request_Factory::getFilter()->filtrate();
		$this->afterFilter();
	}
	
	protected function afterFilter(){
		$this->dispatchEvent('request.afterFilter');
	}
	
	protected function beforeAuthentication(){
		$this->dispatchEvent('request.beforeAuthentication');
	}	
	
	protected function authentication(){
		$this->beforeAuthentication();
		$this->afterAuthentication();
	}
	
	protected function afterAuthentication(){
		$this->dispatchEvent('request.afterAuthentication');
	}	
	
	protected function beforeHandle(){
		$this->dispatchEvent('request.beforeHandle');
	}
    
    protected function handle(){
    	$this->beforeHandle();
		Soul_Request_Factory::getHandler()->handle();
		$this->afterHandle();
    }
    
	protected function afterHandle(){
		$this->dispatchEvent('request.afterHandle');
	}   
	
	protected function beforeOutput(){
		$this->dispatchEvent('request.beforeOutput');
	}
    
    protected function output(){
    	$this->beforeOutput();
    	$this->getContext()->getResponse()->flush();
    	$this->afterOutput();
    }
    
	protected function afterOutput(){
    	$this->dispatchEvent('request.afterOutput');
	}    
    
    protected function finish() {
		
    }    

    public function run() {
		$this->initialize();
		$this->sessionStart();
		$this->route();
		$this->filter();
		$this->authentication();
		$this->handle();
		$this->output();
		$this->finish();
    }
}